#实现一个自动取款机的存取款模拟
#自定义存款账户4个
accounts = [{"userName":"zhangsan","account":"1","pswd":"111","balance":100},
            {"userName":"lisi","account":"2","pswd":"111","balance":100},
            {"userName":"wangwu","account":"3","pswd":"222","balance":100},
            {"userName":"lisi","account":"4","pswd":"333","balance":101}]
def Login(): #账户登陆函数，用来管理账户登陆的操作，判断账户，密码是否正确,返回登录账户的索引或-1（错误信号）
    flag = 0
    n = -1
    while True:
        print("{:=^40}".format('银行用户登录界面'))
        accNums = [accounts[i]["account"]for i in range(len(accounts))]#取出所有账号组成新的列表

        accNum = input("请输入账号：")
        passW= input("请输入密码：")
        print('=' * 40)
        if accNums.count(accNum):
            n = accNums.index(accNum)
            if accounts[n]['pswd'] == passW:
                print("{:=^40}".format( '账户已登录,您可以进行以下操作'))
                print("{:^3}{:^10}{:^3}{:^10}".format("1:", "查询", "2:", "存款"))
                print("{:^3}{:^10}{:^3}{:^10}".format("3:", "取款", "4:", "退出系统"))
                print('=' * 40)
                break
            else:
                flag += 1
                if flag < 3:
                    print("您的账户或密码错误，请重新登陆")
                    continue
                else:
                    print("您暂时无法登陆该系统")
                    break
        else:
            flag += 1
            if flag <3:
                print("您的账户或密码错误，请重新登陆")
                continue
            else:
                print("您暂时无法登陆该系统")
                break
    return (n) #返回账户索引或错误标记

i = Login()
while True:
    if i == -1:
        break
    k = input("请输入您的选择：")
    if k == "1": #打印账户信息
        print("-"*30)
        print("用户名：{}\n账户：{}\n账户余额：{}".format(
                accounts[i]["userName"],accounts[i]["account"],accounts[i]["balance"]))
        print("-"*30)
    elif k == "2":#存钱，并显示账户余额
        print("-" * 30)
        dep_num = int(input("请输入您的存钱金额："))
        accounts[i]["balance"] += dep_num
        print("您的当前账户余额为：{}".format(accounts[i]["balance"]))
        print("-" * 30)
    elif k == "3":#取钱，判断账户余额是否充足，取后显示账户余额
        print("-" * 30)
        draw_num = int(input("请输入您的取钱金额:"))
        if accounts[i]["balance"]<draw_num:
            print("账户余额不足，请重新输入")

        else:
            accounts[i]["balance"] -= draw_num
            print("您的当前账户余额为：{}".format(accounts[i]["balance"]))
        print("-" * 30)
    elif k == "4":
        print("您已退出当前账户")
        break
    else:
        print("输入错误，请重新输入一个数字")
